import React, {useReducer} from 'react'

import useCreateContext from './useCreateContext'

export default function AppReducer(props) {

  const { ctx } = useCreateContext()

  const defaultState = {
    count: 0
  }

  const reducer = (state, action) => {
    switch(action.type) {
      case 'add':
        return {
          count: state.count + 1
        }
      default:
        return state
    }
  }

  const [state, dispatch] = useReducer(reducer, defaultState)

  return (
    <ctx.Provider
      value={{
        state,
        dispatch
      }}
    >
      {props.children}
    </ctx.Provider>
  )
}
